#!/usr/bin/env bash

#-----------------------------------------------------------------------------------------------------
# Nodejs 下载地址
#   镜像1: https://nodejs.org/dist/ (https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.gz)
# OracleJDK 下载地址
#   镜像1: https://www.injdk.cn/ (https://d6.injdk.cn/oraclejdk/11/jdk-11.0.16.1_linux-x64_bin.tar.gz) (https://d6.injdk.cn/oraclejdk/8/jdk-8u341-linux-x64.tar.gz)
#   镜像2: http://www.codebaoku.com/jdk/jdk-index.html (https://114-233-56-217.d.cjjd09.com:30443/123-511/85b2a0b8/1661483-0/85b2a0b8c16863b3ad55e742ac2ba9ff?v=3&t=1667188559&s=f2df45083bb69864446f801bf25b725c&i=dde8a980&filename=jdk-11.0.15.1_linux-x64_bin.tar.gz&d=6ee52da1)
#-----------------------------------------------------------------------------------------------------

[[ -n $DEBUG ]] && set -x
set -o errtrace         # Make sure any error trap is inherited
set -o nounset          # Disallow expansion of unset variables
set -o pipefail         # Use last non-zero exit code in a pipeline

######################################################################################################
# 软件下载路径
######################################################################################################
#--- 运行jenkins-agent的jre
JENKINS_JAVA_URL="http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/OracleJDK/jdk-21.0.3_linux-x64_bin.tar.gz"
#--- jdk (8 | 11 | 17 | 21)
JDK_URL=(
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/OracleJDK/jdk-8u411-linux-x64.tar.gz"
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/OracleJDK/jdk-11.0.23_linux-x64_bin.tar.gz"
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/OracleJDK/jdk-17.0.11_linux-x64_bin.tar.gz"
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/OracleJDK/jdk-21.0.3_linux-x64_bin.tar.gz"
)
#--- gradle (6.8.3 | 7.6.4 | 8.2.1)
GRADLE_URL=(
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/Gradle/gradle-6.8.3-bin.zip"
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/Gradle/gradle-7.6.4-bin.zip"
)
#--- maven (3.8.8 | 3.9.7)
MAVEN_URL=(
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/Maven/apache-maven-3.8.8-bin.tar.gz"
    "http://all.msvc.top:30005/public/app-pkgs/raw/branch/main/Maven/apache-maven-3.9.7-bin.tar.gz"
)
#--- nodejs (16 | 18 | 20)
NODEJS_URL=(
    "https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-x64.tar.gz"
    "https://nodejs.org/dist/v18.20.3/node-v18.20.3-linux-x64.tar.gz"
    "https://nodejs.org/dist/v20.14.0/node-v20.14.0-linux-x64.tar.gz"
)

######################################################################################################
# 脚本设置
######################################################################################################
# 安装目录
INSTALL_DIR="/opt/jenkins-agent"
# 日志文件
LOG_FILE="/tmp/jenkins-agent-install-$(date +"%H%M%S").log"
# 安装步骤执行标记文件
SETUP_STATE_FILE="/tmp/jenkins-agent-install-setup.txt"
# sudo用户名密码
SUDO_TAG="0"
SUDO_USER="${SUDO_USER:-root}"
SUDO_PASSWORD="${SUDO_PASSWORD:-}"
# 执行 install 指令
INSTALL_TAG="0"
# 安装 jenkins-agent
JENKINS_AGENT=""
JENKINS_AGENT_JNLP_URL=""
JENKINS_AGENT_SECRET=""
# 安装git
GIT_TAG="0"
# 安装java
JAVA_TAG="0"
# 安装gradle
GRADLE_TAG="0"
# 安装maven
MAVEN_TAG="0"
# 安装nodejs
NODEJS_TAG="0"
# 当前脚本文件名称(仅显示)
SCRIPT_FILE_NAME="jenkins-agent-install.sh"

######################################################################################################
# 临时变量
######################################################################################################
# 存储所有命令行参数
SCRIPT_PARAMETER="$*"
# 存储命令执行返回值的的临时变量
COMMAND_OUTPUT=""
# 普通日志信息
ACCESS_INFO="\n\033[32mACCESS Summary: \033[0m\n  "
# 错误日志信息
ERROR_INFO="\n\033[31mERROR Summary: \033[0m\n  "
# 执行过程中存在错误的标识
HAS_ERROR="0"

######################################################################################################
# 通用函数
######################################################################################################

trap trap::info 1 2 3 15 EXIT

# 信号处理
function trap::info() {
    [[ ${#ERROR_INFO} -gt 37 ]] && echo -e "$ERROR_INFO"
    [[ ${#ACCESS_INFO} -gt 38 ]] && echo -e "$ACCESS_INFO"
    [ -f "$LOG_FILE" ] && echo -e "\n\n  See detailed log >>> cat $LOG_FILE \n\n"
    trap '' EXIT
    exit
}

# 错误日志
function log::error() {
    HAS_ERROR="1"
    local item; item="[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')]: \033[31mERROR:   \033[0m$*"
    ERROR_INFO="${ERROR_INFO}${item}\n  "
    echo -e "${item}" | tee -a "$LOG_FILE"
}

# 基础日志
function log::info() {
    printf "[%s]: \033[32mINFO:    \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
}

# 警告日志
function log::warning() {
    printf "[%s]: \033[33mWARNING: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
}

# 访问信息
function log::access() {
    ACCESS_INFO="${ACCESS_INFO}$*\n  "
    printf "[%s]: \033[32mINFO:    \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" | tee -a "$LOG_FILE"
}

# 执行日志
function log::exec() {
    printf "[%s]: \033[34mEXEC:    \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" >> "$LOG_FILE"
}

# 转义引号
function utils::quote() {
    # shellcheck disable=SC2046
    if [ $(echo "$*" | tr -d "\n" | wc -c) -eq 0 ]; then
        echo "''"
    elif [ $(echo "$*" | tr -d "[a-z][A-Z][0-9]:,.=~_/\n-" | wc -c) -gt 0 ]; then
        printf "%s" "$*" | sed -e "1h;2,\$H;\$!d;g" -e "s/'/\'\"\'\"\'/g" | sed -e "1h;2,\$H;\$!d;g" -e "s/^/'/g" -e "s/$/'/g"
    else
        echo "$*"
    fi
}

# 本地执行命令
function command::exec() {
    local command="$*"
    if [[ "${SUDO_TAG:-}" == "1" ]]; then
        sudo_options="sudo -H -n -u ${SUDO_USER}"
        if [[ "${SUDO_PASSWORD:-}" != "" ]]; then
            sudo_options="${sudo_options// -n/} -p \"\" -S <<< \"${SUDO_PASSWORD}\""
        fi
        command="$sudo_options bash -c $(utils::quote "$command")"
    fi
    command="$(utils::quote "$command")"
    log::exec "[command]" "bash -c $(printf "%s" "${command//${SUDO_PASSWORD:-}/zzzzzz}")"
    # shellcheck disable=SC2094
    COMMAND_OUTPUT=$(eval bash -c "${command}" 2>> "$LOG_FILE" | tee -a "$LOG_FILE")
    local status=$?
    return $status
}

# 检查返回码
function check::exit_code() {
    local code=${1:-}
    local app=${2:-}
    local desc=${3:-}
    local exit_script=${4:-}
    if [[ "${code}" == "0" ]]; then
        log::info "[${app}]" "${desc} succeeded."
    else
        log::error "[${app}]" "${desc} failed."
        [[ "$exit_script" == "exit" ]] && exit "$code"
    fi
}

# 检查命令是否存在
function check::command_exists() {
    local cmd=${1}
    local package=${2}
    if command -V "$cmd" > /dev/null 2>&1; then
        log::info "[check]" "$cmd command exists."
    else
        log::warning "[check]" "I require $cmd but it's not installed."
        log::warning "[check]" "install $package package."
        command::exec "yum install -y ${package}"
        check::exit_code "$?" "check" "$package install" "exit"
    fi
}

# 下载文件
function utils::download_file() {
    local url="$1"
    local dest="$2"
    local unzip_tag="${3:-1}"
    local dest_dirname; dest_dirname=$(dirname "$dest")
    local filename; filename=$(basename "$dest")
    log::info "[download]" "${filename}"
    command::exec "
        set -e
        if [ ! -f \"${dest}\" ]; then
            [ ! -d \"${dest_dirname}\" ] && mkdir -pv \"${dest_dirname}\"
            wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate \"${url}\" -O \"${dest}\"
            if [[ \"${unzip_tag}\" == \"unzip\" ]]; then
                command -v unzip 2>/dev/null || yum install -y unzip
                unzip -o \"${dest}\" -d \"${dest_dirname}\"
            fi
        else
            echo \"${dest} is exists!\"
        fi
    "
    local status="$?"
    check::exit_code "$status" "download" "${filename}" "exit"
    return "$status"
}

# 检查用到的命令
function check::command() {
  check::command_exists tar tar
  check::command_exists wget wget
  check::command_exists unzip unzip
}

######################################################################################################
# 安装函数
######################################################################################################

# 预检
function check::preflight() {
    # check command
    check::command
}

# 创建文件夹
function install::mkdir() {
    log::info "[mkdir]" "创建目录..."
    command::exec "
        sudo mkdir -p ${INSTALL_DIR}
        sudo chown -R \$(id -u):\$(id -g) ${INSTALL_DIR}
    "
    check::exit_code "$?" "mkdir" "创建目录:${INSTALL_DIR}" "exit"
    log::info "[mkdir]" "创建目录成功!"
}

# 下载
function install::jenkins_agent() {
    log::info "[jenkins-agent]" "下载jenkins-agent..."
    local tmp_download="tmp_download"
    local tmp_dir="tmp"
    local java_mem_opts='-server -Xmx1g'
    local database_opts=' -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false'
    local java_opts_ext=' -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai'
    command::exec "
        mkdir -p ${INSTALL_DIR}/workspace
        java_bin=\"\"
        agent_file=\"\"
        # 安装JRE
        mkdir -p ${INSTALL_DIR}/jenkins-agent-jre
        cd ${INSTALL_DIR}/jenkins-agent-jre
        rm -rf ./${tmp_download}
        mkdir ./${tmp_download}
        cd ./${tmp_download}
        wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${JENKINS_JAVA_URL}
        file_name=\`ls ./ | head -n 1\`
        if [ -z \$file_name ];then
            echo '下载失败=${JENKINS_JAVA_URL}'
            cd ../
        else
            mkdir ./${tmp_dir}
            tar -zxvf \$file_name -C ./${tmp_dir}
            mv \$file_name ../\$file_name
            cd ./${tmp_dir}
            file_name=\`ls ./ | head -n 1\`
            mv \$file_name ../../\$file_name
            cd ../../
            java_bin=\"${INSTALL_DIR}/jenkins-agent-jre/\$file_name/bin/java\"
            \$file_name/bin/java -version
        fi
        rm -rf ./${tmp_download}
        # 安装 jenkins-agent
        cd ${INSTALL_DIR}
        rm -rf ./${tmp_download}
        mkdir ./${tmp_download}
        cd ./${tmp_download}
        wget --timeout=10 --no-check-certificate ${JENKINS_AGENT}
        file_name=\`ls ./ | head -n 1\`
        if [ -z \$file_name ];then
            echo '下载失败=${JENKINS_AGENT}'
        else
            mv \$file_name ../\$file_name
            agent_file=\"${INSTALL_DIR}/\$file_name\"
        fi
        cd ../
        rm -rf ./${tmp_download}
        # 写文件start.sh文件
        cat << EOF > ${INSTALL_DIR}/start.sh
#!/bin/bash

jnlpUrl='${JENKINS_AGENT_JNLP_URL}'
secret='${JENKINS_AGENT_SECRET}'
workDir='${INSTALL_DIR}/workspace'

#-------------------------------------------------------------------
java='\$java_bin'
agent_file='\$agent_file'
java_mem_opts=' -DappName=jenkins-agent $java_mem_opts'
database_opts='$database_opts'
java_opts_ext='$java_opts_ext'
#-------------------------------------------------------------------

pid=\\\`ps -ef | grep 'DappName=jenkins-agent ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
if [ -z \\\$pid ];then
    nohup \\\$java \\\$java_mem_opts \\\$database_opts \\\$java_opts_ext -jar \\\$agent_file -jnlpUrl \\\$jnlpUrl -secret \\\$secret -workDir \\\$workDir >${INSTALL_DIR}/agent-logs.log 2>&1 &
    echo 'jenkins-agent启动成功!'
else
    echo 'jenkins-agent正在运行...'
fi
echo '查看日志:  tail -F ${INSTALL_DIR}/agent-logs.log -n 100'
EOF
        # 写文件kill.sh文件
        cat << EOF > ${INSTALL_DIR}/kill.sh
#!/bin/bash

pid=\\\`ps -ef | grep 'DappName=jenkins-agent ' | grep -v 'grep' | awk '{print \\\$2}'\\\`
if [ -z \\\$pid ];then
    echo 'jenkins-agent未运行'
else
    ps -ef | grep 'DappName=jenkins-agent ' | grep -v 'grep' | awk '{print \\\$2}' | xargs kill
    echo 'jenkins-agent已停止!'
fi
EOF
        chmod +x ${INSTALL_DIR}/start.sh
        chmod +x ${INSTALL_DIR}/kill.sh
        sudo chown -R \$(id -u):\$(id -g) ${INSTALL_DIR}
    "
    check::exit_code "$?" "jenkins-agent" "下载jenkins-agent" "exit"
    log::info "[jenkins-agent]" "jenkins-agent下载成功!"
}

# 安装git
function install::git() {
    log::info "[git]" "安装git..."
    command::exec "
        yum -y install git
        git --version
        git config --global credential.helper store
    "
    check::exit_code "$?" "git" "安装git" "exit"
    log::info "[git]" "git安装成功!"
}

# 安装java
function install::java() {
    log::info "[java]" "安装java..."
    local tmp_download="tmp_download"
    local tmp_dir="tmp"
    for url in "${JDK_URL[@]}"; do
        command::exec "
            mkdir -p ${INSTALL_DIR}/env/java
            cd ${INSTALL_DIR}/env/java
            rm -rf ./${tmp_download}
            mkdir ./${tmp_download}
            cd ./${tmp_download}
            wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${url}
            file_name=\`ls ./ | head -n 1\`
            if [ -z \$file_name ];then
                echo '下载失败=${url}'
                cd ../
            else
                mkdir ./${tmp_dir}
                tar -zxvf \$file_name -C ./${tmp_dir}
                mv \$file_name ../\$file_name
                cd ./${tmp_dir}
                file_name=\`ls ./ | head -n 1\`
                mv \$file_name ../../\$file_name
                cd ../../
            fi
            rm -rf ./${tmp_download}
        "
        check::exit_code "$?" "java" "安装java: $url" "exit"
    done
    log::info "[java]" "java安装成功!"
}

# 安装gradle
function install::gradle() {
    log::info "[gradle]" "安装gradle..."
    local tmp_download="tmp_download"
    local tmp_dir="tmp"
    for url in "${GRADLE_URL[@]}"; do
        command::exec "
            mkdir -p ${INSTALL_DIR}/env/gradle
            cd ${INSTALL_DIR}/env/gradle
            rm -rf ./${tmp_download}
            mkdir ./${tmp_download}
            cd ./${tmp_download}
            wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${url}
            file_name=\`ls ./ | head -n 1\`
            if [ -z \$file_name ];then
                echo '下载失败=${url}'
                cd ../
            else
                mkdir ./${tmp_dir}
                unzip \$file_name -d ./${tmp_dir}
                mv \$file_name ../\$file_name
                cd ./${tmp_dir}
                file_name=\`ls ./ | head -n 1\`
                mv \$file_name ../../\$file_name
                cd ../../
            fi
            rm -rf ./${tmp_download}
        "
        check::exit_code "$?" "gradle" "安装gradle: $url" "exit"
    done
    log::info "[gradle]" "gradle安装成功!"
}

# 安装maven
function install::maven() {
    log::info "[maven]" "安装maven..."
    local tmp_download="tmp_download"
    local tmp_dir="tmp"
    for url in "${MAVEN_URL[@]}"; do
        command::exec "
            mkdir -p ${INSTALL_DIR}/env/maven
            cd ${INSTALL_DIR}/env/maven
            rm -rf ./${tmp_download}
            mkdir ./${tmp_download}
            cd ./${tmp_download}
            wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${url}
            file_name=\`ls ./ | head -n 1\`
            if [ -z \$file_name ];then
                echo '下载失败=${url}'
                cd ../
            else
                mkdir ./${tmp_dir}
                tar -zxvf \$file_name -C ./${tmp_dir}
                mv \$file_name ../\$file_name
                cd ./${tmp_dir}
                file_name=\`ls ./ | head -n 1\`
                mv \$file_name ../../\$file_name
                cd ../../
            fi
            rm -rf ./${tmp_download}
        "
        check::exit_code "$?" "maven" "安装maven: $url" "exit"
    done
    log::info "[maven]" "maven安装成功!"
}

# 安装nodejs
function install::nodejs() {
    log::info "[nodejs]" "安装nodejs..."
    local tmp_download="tmp_download"
    local tmp_dir="tmp"
    for url in "${NODEJS_URL[@]}"; do
        command::exec "
            mkdir -p ${INSTALL_DIR}/env/nodejs
            cd ${INSTALL_DIR}/env/nodejs
            rm -rf ./${tmp_download}
            mkdir ./${tmp_download}
            cd ./${tmp_download}
            wget --timeout=10 --waitretry=3 --tries=5 --retry-connrefused --no-check-certificate ${url}
            file_name=\`ls ./ | head -n 1\`
            if [ -z \$file_name ];then
                echo '下载失败=${url}'
                cd ../
            else
                mkdir ./${tmp_dir}
                tar -zxvf \$file_name -C ./${tmp_dir}
                mv \$file_name ../\$file_name
                cd ./${tmp_dir}
                file_name=\`ls ./ | head -n 1\`
                mv \$file_name ../../\$file_name
                cd ../../
            fi
            rm -rf ./${tmp_download}
        "
        check::exit_code "$?" "nodejs" "安装nodejs: $url" "exit"
    done
    log::info "[nodejs]" "安装nodejs成功!"
}

######################################################################################################
# 主调用逻辑
######################################################################################################

# 安装软件
function install() {
    # 创建安装步骤执行标记文件
    [ ! -f "${SETUP_STATE_FILE}" ] && echo "" > $SETUP_STATE_FILE
    log::info "[setup]" "setup file: $SETUP_STATE_FILE"
    install::mkdir
    # 更新yum源
    if [[ "${UPDATE_YUM_REPOS:-}" == "1" ]]; then
        if grep -q "install_jenkins_agent" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 jenkins_agent"
        else
            install::jenkins_agent
            echo "install_jenkins_agent" >> $SETUP_STATE_FILE
        fi
    fi
    # 安装git环境
    if [[ "${GIT_TAG:-}" == "1" ]]; then
        if grep -q "install_git" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 git"
        else
            install::git
            echo "install_git" >> $SETUP_STATE_FILE
        fi
    fi
    # 安装java环境
    if [[ "${JAVA_TAG:-}" == "1" ]]; then
        if grep -q "install_java" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 java"
        else
            install::java
            echo "install_java" >> $SETUP_STATE_FILE
        fi
    fi
    # 安装gradle环境
    if [[ "${GRADLE_TAG:-}" == "1" ]]; then
        if grep -q "install_gradle" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 gradle"
        else
            install::gradle
            echo "install_gradle" >> $SETUP_STATE_FILE
        fi
    fi
    # 安装maven环境
    if [[ "${MAVEN_TAG:-}" == "1" ]]; then
        if grep -q "install_maven" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 maven"
        else
            install::maven
            echo "install_maven" >> $SETUP_STATE_FILE
        fi
    fi
    # 安装nodejs环境
    if [[ "${NODEJS_TAG:-}" == "1" ]]; then
        if grep -q "install_nodejs" $SETUP_STATE_FILE; then
            log::info "[skip]" "跳过已完成步骤 nodejs"
        else
            install::nodejs
            echo "install_nodejs" >> $SETUP_STATE_FILE
        fi
    fi
}

######################################################################################################
# main-入口
######################################################################################################

# 使用帮助
function help::usage() {
    cat << EOF
Usage:
  $SCRIPT_FILE_NAME [command]

Available Commands:
  install   安装jenkins-agent和项目编译环境
Flag:
  --dir         安装路径, 默认: '$INSTALL_DIR'
  --agent       jenkins-agent下载地址(\${JENKINS_URL}/jnlpJars/agent.jar)， 默认: '$JENKINS_AGENT'
  --jnlpUrl     jenkins-agent的jnlpUrl参数(\${JENKINS_URL}/computer/\${AGENT_NAME}/jenkins-agent.jnlp)， 默认: '$JENKINS_AGENT_JNLP_URL'
  --secret      jenkins-agent的secret参数， 默认: '$JENKINS_AGENT_SECRET'
  --git         是否安装git, 默认: '$GIT_TAG'
  --java        是否安装java, 默认: '$JAVA_TAG'
  --gradle      是否安装gradle, 默认: '$GRADLE_TAG'
  --maven       是否安装maven, 默认: '$MAVEN_TAG'
  --nodejs      是否安装nodejs, 默认: '$NODEJS_TAG'

Example:
  $SCRIPT_FILE_NAME install \\
    --install-dir '/data/jenkins-agent' \\
    --agent '...' \\
    --jnlpUrl '...' \\
    --secret '...' \\
    --git 0 \\
    --java 1 \\
    --gradle 1 \\
    --maven 1 \\
    --nodejs 1

EOF
    exit 1
}

# 参数处理
[ "$#" == "0" ] && help::usage
while [ "${1:-}" != "" ]; do
    case $1 in
        # -------------------------------------------------------------- 指令
        install )               INSTALL_TAG="1"
                                ;;
        # -------------------------------------------------------------- 指令参数
        --dir )                 shift
                                INSTALL_DIR=${1:-INSTALL_DIR}
                                ;;
        --agent )               shift
                                JENKINS_AGENT=${1:-JENKINS_AGENT}
                                ;;
        --jnlpUrl )             shift
                                JENKINS_AGENT_JNLP_URL=${1:-JENKINS_AGENT_JNLP_URL}
                                ;;
        --secret )              shift
                                JENKINS_AGENT_SECRET=${1:-JENKINS_AGENT_SECRET}
                                ;;
        --git )                 shift
                                GIT_TAG="1"
                                ;;
        --java )                shift
                                JAVA_TAG="1"
                                ;;
        --gradle )              shift
                                GRADLE_TAG="1"
                                ;;
        --maven )               shift
                                MAVEN_TAG="1"
                                ;;
        --nodejs )              shift
                                NODEJS_TAG="1"
                                ;;
        * )                     help::usage
                                exit 1
    esac
    shift
done

# 开始
log::info "[start]" "bash $SCRIPT_FILE_NAME ${SCRIPT_PARAMETER//${SUDO_PASSWORD:-}/zzzzzz}"
# 预检
check::preflight
# 动作
if [[ "${INSTALL_TAG:-}" == "1" ]]; then
    install
else
    help::usage
fi

# bash <(curl -s https://gitee.com/LiZhiW/helm-chart/raw/master/01shell/jenkins-agent-install.sh) [cmd]
